﻿#region Copyright (c) 2013-04, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Threading.Tasks;
using NUnit.Framework;


namespace Amarok.Agents
{
	[TestFixture]
	public class Test_AgentReference
	{
		private class MyMessage : Message
		{
		}

		private class DummySubscription : ICompletable
		{
			public Task Completion
			{
				get
				{
					throw new NotImplementedException();
				}
			}

			public void Complete(Boolean discardAvailableMessages = false)
			{
				throw new NotImplementedException();
			}

			public void Dispose()
			{
				throw new NotImplementedException();
			}
		}

		private class MyAgent : IAgent
		{
			public static Int32 PostCalled;
			public static Int32 DisposeCalled;
			public static Int32 CompleteCalled;
			public static Int32 SubscribeAgentCalled;
			public static Int32 SubscribeCallbackCalled;

			public Boolean Post(Message message)
			{
				PostCalled++;
				return true;
			}

			public void Dispose()
			{
				DisposeCalled++;
			}

			public Task Completion
			{
				get
				{
					throw new NotImplementedException();
				}
			}

			public void Complete(Boolean discardPendingMessages = false)
			{
				CompleteCalled++;
			}

			public ICompletable Subscribe(Type messageType, AgentReference subscriber, Predicate<Message> messageFilter = null)
			{
				SubscribeAgentCalled++;
				return new DummySubscription();
			}

			public ICompletable Subscribe(Type messageType, Action<Message> subscriber, Predicate<Message> messageFilter = null)
			{
				SubscribeCallbackCalled++;
				return new DummySubscription();
			}
		}

		[SetUp]
		public void Setup()
		{
			MyAgent.PostCalled = 0;
			MyAgent.DisposeCalled = 0;
			MyAgent.CompleteCalled = 0;
			MyAgent.SubscribeAgentCalled = 0;
			MyAgent.SubscribeCallbackCalled = 0;
		}


		// --- TESTS ---


		[Test]
		public void Test_Construction()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			Assert.That(agentReference.IsAlive, Is.True);
			Assert.That(agentReference.Target, Is.SameAs(agent));
			Assert.That(agentReference.Completion, Is.Not.Null);
			Assert.That(agentReference.Completion.IsCompleted, Is.False);

			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));

			GC.KeepAlive(agent);
		}

		[Test]
		public void Test_Construction_Null()
		{
			Assert.Throws<ArgumentNullException>(() =>
				{
					var agentReference = new AgentReference(null);
				});
		}

		[Test]
		public void Test_Post()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));

			var result = agentReference.Post(new MyMessage());

			Assert.That(result, Is.True);

			Assert.That(MyAgent.PostCalled, Is.EqualTo(1));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));

			GC.KeepAlive(agent);
		}

		[Test]
		public void Test_Post_NullMessage()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			Assert.Throws<ArgumentNullException>(() =>
				{
					agentReference.Post(null);
				});

			GC.KeepAlive(agent);
		}

		[Test]
		public void Test_Subscribe_Agent()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));

			agentReference.Subscribe(
				typeof(Message),
				new MyAgent().ToAgentReference());

			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(1));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));

			GC.KeepAlive(agent);
		}

		[Test]
		public void Test_Subscribe_Agent_NullType()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			Assert.Throws<ArgumentNullException>(() =>
				{
					agentReference.Subscribe(
						null,
						new MyAgent().ToAgentReference());
				});

			GC.KeepAlive(agent);
		}

		[Test]
		public void Test_Subscribe_Agent_NonMesageType()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			Assert.Throws<ArgumentException>(() =>
				{
					agentReference.Subscribe(
						typeof(Version),
						new MyAgent().ToAgentReference());
				});

			GC.KeepAlive(agent);
		}

		[Test]
		public void Test_Subscribe_Agent_NullAgent()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			Assert.Throws<ArgumentNullException>(() =>
				{
					agentReference.Subscribe(
						typeof(Message),
						(AgentReference)null);
				});

			GC.KeepAlive(agent);
		}

		[Test]
		public void Test_Subscribe_Callback()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));

			agentReference.Subscribe(typeof(Message),
				message =>
				{
				});

			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(1));

			GC.KeepAlive(agent);
		}

		[Test]
		public void Test_Subscribe_Callback_NullType()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			Assert.Throws<ArgumentNullException>(() =>
				{
					agentReference.Subscribe(
						null,
						message =>
						{
						});
				});

			GC.KeepAlive(agent);
		}

		[Test]
		public void Test_Subscribe_Callback_NonMessageType()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			Assert.Throws<ArgumentException>(() =>
				{
					agentReference.Subscribe(
						typeof(Version),
						message =>
						{
						});
				});

			GC.KeepAlive(agent);
		}

		[Test]
		public void Test_Subscribe_Callback_NullCallback()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			Assert.Throws<ArgumentNullException>(() =>
				{
					agentReference.Subscribe(
						typeof(Message),
						(Action<Message>)null);
				});

			GC.KeepAlive(agent);
		}

		[Test]
		public void Test_State_AfterBeingGarbageCollected()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			agent = null;

			GC.Collect(2, GCCollectionMode.Forced);
			Helper.WaitForCondition(5000, 100, () => agentReference.IsAlive == false);

			Assert.That(agentReference.IsAlive, Is.False);
			Assert.That(agentReference.Target, Is.Null);

			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));
		}

		[Test]
		public void Test_Post_AfterBeingGarbageCollected()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			var completionTask = agentReference.Completion;
			agent = null;

			GC.Collect(2, GCCollectionMode.Forced);
			Helper.WaitForCondition(5000, 100, () => agentReference.IsAlive == false);

			var result = agentReference.Post(new MyMessage());

			Assert.That(result, Is.False);

			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));
			Assert.That(completionTask.IsCompleted, Is.True);
		}

		[Test]
		public void Test_Subscribe_Agent_AfterBeingGarbageCollected()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			var completionTask = agentReference.Completion;
			agent = null;

			GC.Collect(2, GCCollectionMode.Forced);
			Helper.WaitForCondition(5000, 100, () => agentReference.IsAlive == false);

			agentReference.Subscribe(
				typeof(Message),
				new MyAgent().ToAgentReference());

			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));
			Assert.That(completionTask.IsCompleted, Is.True);
		}

		[Test]
		public void Test_Subscribe_Callback_AfterBeingGarbageCollected()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			var completionTask = agentReference.Completion;
			agent = null;

			GC.Collect(2, GCCollectionMode.Forced);
			Helper.WaitForCondition(5000, 100, () => agentReference.IsAlive == false);

			agentReference.Subscribe(
				typeof(Message),
				message =>
				{
				});

			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));
			Assert.That(completionTask.IsCompleted, Is.True);
		}

		[Test]
		public void Test_Complete_AfterBeingGarbageCollected()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			var completionTask = agentReference.Completion;
			agent = null;

			GC.Collect(2, GCCollectionMode.Forced);
			Helper.WaitForCondition(5000, 100, () => agentReference.IsAlive == false);

			agentReference.Complete();

			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));
			Assert.That(completionTask.IsCompleted, Is.True);
		}

		[Test]
		public void Test_Dispose_AfterBeingGarbageCollected()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			var completionTask = agentReference.Completion;
			agent = null;

			GC.Collect(2, GCCollectionMode.Forced);
			Helper.WaitForCondition(5000, 100, () => agentReference.IsAlive == false);

			agentReference.Dispose();

			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));
			Assert.That(completionTask.IsCompleted, Is.True);
		}

		[Test]
		public void Test_Completion_AfterBeingGarbageCollected()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);

			agent = null;

			GC.Collect(2, GCCollectionMode.Forced);
			Helper.WaitForCondition(5000, 100, () => agentReference.IsAlive == false);

			Assert.That(agentReference.Completion.Wait(5000), Is.True);
			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));
		}

		[Test]
		public void Test_Usage()
		{
			IAgent agent = new MyAgent();
			IAgent implicitAgentReference = new AgentReference(new MyAgent());
			AgentReference explicitAgentReference = new AgentReference(new MyAgent());

			agent = new MyAgent();
			implicitAgentReference = new AgentReference(new MyAgent());
			explicitAgentReference = new AgentReference(new MyAgent());

			agent.Post(new MyMessage());
			implicitAgentReference.Post(new MyMessage());
			explicitAgentReference.Post(new MyMessage());

			agent.Complete();
			implicitAgentReference.Complete();
			explicitAgentReference.Complete();

			agent = null;
			implicitAgentReference = null;
			explicitAgentReference = null;
		}

		[Test]
		public void Test_Dispose()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);
			var completionTask = agentReference.Completion;

			Assert.That(agentReference.IsAlive, Is.True);
			Assert.That(agentReference.Target, Is.Not.Null);
			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));
			Assert.That(completionTask.IsCompleted, Is.False);

			agentReference.Dispose();

			Assert.That(agentReference.IsAlive, Is.False);
			Assert.That(agentReference.Target, Is.Null);
			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));
			Assert.That(completionTask.IsCompleted, Is.True);

			GC.KeepAlive(agent);
		}

		[Test]
		public void Test_Complete_DiscardAvailableMessages()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);
			var completionTask = agentReference.Completion;

			Assert.That(agentReference.IsAlive, Is.True);
			Assert.That(agentReference.Target, Is.Not.Null);
			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));
			Assert.That(completionTask.IsCompleted, Is.False);

			agentReference.Complete(discardAvailableMessages: true);

			Assert.That(agentReference.IsAlive, Is.False);
			Assert.That(agentReference.Target, Is.Null);
			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));
			Assert.That(completionTask.IsCompleted, Is.True);

			GC.KeepAlive(agent);
		}

		[Test]
		public void Test_Complete_ProcessAvailableMessages()
		{
			var agent = new MyAgent();
			var agentReference = new AgentReference(agent);
			var completionTask = agentReference.Completion;

			Assert.That(agentReference.IsAlive, Is.True);
			Assert.That(agentReference.Target, Is.Not.Null);
			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));
			Assert.That(completionTask.IsCompleted, Is.False);

			agentReference.Complete(discardAvailableMessages: false);

			Assert.That(agentReference.IsAlive, Is.False);
			Assert.That(agentReference.Target, Is.Null);
			Assert.That(MyAgent.PostCalled, Is.EqualTo(0));
			Assert.That(MyAgent.DisposeCalled, Is.EqualTo(0));
			Assert.That(MyAgent.CompleteCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeAgentCalled, Is.EqualTo(0));
			Assert.That(MyAgent.SubscribeCallbackCalled, Is.EqualTo(0));
			Assert.That(completionTask.IsCompleted, Is.True);

			GC.KeepAlive(agent);
		}

	}
}

#endif
